package com.idanchuang.component.just.web;

import com.google.common.collect.Lists;
import com.idanchuang.component.just.web.swagger.SwaggerConfigBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.BasicAuth;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Swagger2配置类
 * 在与spring boot集成时，放在与Application.java同级的目录下。
 * 通过@Configuration注解，让Spring来加载该类配置。
 * 再通过@EnableSwagger2注解来启用Swagger2。
 * @author yjy
 * @date 2019/10/17 14:11
 **/
@Configuration
@EnableSwagger2
@ServletComponentScan("com.idanchuang.component.just.web.swagger")
@ConditionalOnClass(Docket.class)
@ConditionalOnProperty(value = "spring.swagger2.enabled", havingValue = "true")
public class Swagger2AutoConfiguration {

    @Bean("acgSwaggerConfigBean")
    @ConfigurationProperties("spring.swagger2")
    public SwaggerConfigBean swaggerConfigBean() {
        return new SwaggerConfigBean();
    }

    /**
     * 创建API应用
     * apiInfo() 增加API相关信息
     * 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现，
     * 本例采用指定扫描的包路径来定义指定要建立API的目录。
     *
     * @return ..
     */
    @Bean
    @ConditionalOnMissingBean(Docket.class)
    public Docket createRestApi(SwaggerConfigBean swaggerConfigBean) {
        if (swaggerConfigBean.getControllerPackage() == null) {
            throw new RuntimeException("You enabled swagger2, but have not assign the controllerPackage!");
        }
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo(swaggerConfigBean))
                .select()
                .apis(RequestHandlerSelectors.basePackage(swaggerConfigBean.getControllerPackage()))
                .paths(PathSelectors.any())
                .build();
        docket.securitySchemes(Lists.newArrayList(new BasicAuth("basicAuth")));

        return docket;
    }

    /**
     * 创建该API的基本信息（这些基本信息会展现在文档页面中）
     * 访问地址：http://项目实际地址/swagger-ui.html
     * @return ..
     */
    private ApiInfo apiInfo(SwaggerConfigBean swaggerConfigBean) {
        return new ApiInfoBuilder()
                .title(swaggerConfigBean.getTitle() != null ? swaggerConfigBean.getTitle() : "未指定文档标题")
                .description(swaggerConfigBean.getDescription() != null ? swaggerConfigBean.getDescription() : "-")
                .termsOfServiceUrl(swaggerConfigBean.getTermsOfServiceUrl() != null ? swaggerConfigBean.getTermsOfServiceUrl() : "-")
                .contact(new Contact("-", "-", "-"))
                .version(swaggerConfigBean.getVersion() != null ? swaggerConfigBean.getVersion() : "1.0")
                .build();
    }


}
